package com.gzhryc.shared_device.oem.h5.mng.controllers.cdb;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBOrder;
import com.gzhryc.shared_device.cdb.code.dao.db.PortableBattery;
import com.gzhryc.shared_device.cdb.code.dao.enums.BatteryCabinetEnum;
import com.gzhryc.shared_device.cdb.code.services.CDBOrderService;
import com.gzhryc.shared_device.cdb.code.services.PortableBatteryService;
import com.gzhryc.shared_device.cdb.code.services.dto.CDBOrderSearch;
import com.gzhryc.shared_device.cdb.code.services.dto.PortableBatterySearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

public class PortableBatteryController {

    @Route("/cdb/device/portable_battery/list.html")
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", BatteryCabinetEnum.State.values());
        return "/cdb/device/portable_battery/list.html";
    }

    @Route(value = "/cdb/device/portable_battery/list.data")
    public ListResult<PortableBattery> listData(HttpServletRequest request) {
        ListResult<PortableBattery> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        PortableBatterySearch search = WebUtils.getParameter(request, PortableBatterySearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(PortableBatteryService.self().countBySearch(search));
        List<PortableBattery> batteryCabinetList = PortableBatteryService.self().findBySearch(search, pageNo, pageLimit);
        if (batteryCabinetList != null && batteryCabinetList.size() > 0) {
            result.setCode(DtoConstants.SUCCESS, null);
            result.getBody().setData(batteryCabinetList);
        }
        return result;
    }

    @Route("/cdb/device/portable_battery/info.html")
    public String info(HttpServletRequest request) {
        String deviceSn = WebUtils.getString(request,"deviceSn");
        PortableBattery portableBattery = PortableBatteryService.self().getByDeviceSn(deviceSn);
        WebUtils.setAttribute(request,portableBattery);
        return "/cdb/device/portable_battery/info.html";
    }

    @Route(value = "/cdb/device/portable_battery/order/list.data")
    public ListResult<CDBOrder> orderListData(HttpServletRequest request) {
        ListResult<CDBOrder> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        CDBOrderSearch search = WebUtils.getParameter(request, CDBOrderSearch.class);
        if(StringTools.isNotBlank(search.getPortableBatterySn())) {
            Integer pageNo = WebUtils.getInteger(request, "page", 1);
            Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

            result.getBody().setTotal(CDBOrderService.self(new Date()).countBySearch(search));
            List<CDBOrder> cdbOrderList = CDBOrderService.self(new Date()).findBySearch(search, pageNo, pageLimit);
            if (cdbOrderList != null && cdbOrderList.size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
                result.getBody().setData(cdbOrderList);
            }
        }
        return result;
    }

    @Route(value = {"/cdb/device/portable_battery/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除充电宝失败");
        String deviceSn = WebUtils.getString(request,"deviceSn");
        try {
            if (PortableBatteryService.self().deleteByDeviceSn(deviceSn)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

}
